<?php
/**
 * @license http://www.gnu.org/licenses/lgpl.html LGPL
 * @author Harlan Iverson <h dot iverson at gmail dot com>
 * @copyright Copyright (c) 2006, Harlan Iverson.
 * @version $Id: test-facelets2smarty.php 20 2006-08-01 05:54:40Z Harlan $
 */
 
 


require_once 'Pearified/Smarty/Smarty.class.php';
require_once 'PhpFaces/ViewHandler.class.php';
require_once 'PHPUnit.php';

define( "SMARTY_TEMPLATES_C", "templates_c" );
function PhpFaces_setUp_smarty( &$smarty ) {
	if(!file_exists( SMARTY_TEMPLATES_C )) {
		mkdir( SMARTY_TEMPLATES_C );
	}
	$smarty->template_dir=".";
	$smarty->templates_c = SMARTY_TEMPLATES_C;
}

function PhpFaces_tearDown_smarty( &$smarty ) {
	//PhpFaces_remove_directory( SMARTY_TEMPLATES_C );
}

function PhpFaces_remove_directory($dir) {
     if(substr($dir, -1, 1) == "/"){
   $dir = substr($dir, 0, strlen($dir) - 1);
 }
 if ($handle = opendir("$dir")) {
   while (false !== ($item = readdir($handle))) {
     if ($item != "." && $item != "..") {
       if (is_dir("$dir/$item")) { PhpFaces_remove_directory("$dir/$item");  }
       else { unlink("$dir/$item"); }
         }
       }
       closedir($handle);
       rmdir($dir);
     }
   }


class Facelets2SmartyTest extends PHPUnit_TestCase {

	var $smarty;

	function __construct($name) {
		$this->PHPUnit_TestCase($name);
	}
	
	function setUp() {
		$this->smarty = new Smarty();
		PhpFaces_setUp_smarty( $this->smarty );
	}
	
	function tearDown() {
		PhpFaces_tearDown_smarty( $this->smarty );
	}

	
	
	

	/*function testJSFC() {
		$this->useFile( "pages/facelets-test-jsfc.xml" );
	}*/
	
	
	function testUiDefine() {
		$this->useFile( "pages/ui-define.xml" );
	}
	
	function testRawEL() {
		$this->useFile( "pages/raw-el.xml", true );
	}
	
	function testUiInsert() {
		$this->useFile( "pages/ui-insert.xml" );
	}
	
	function testUiInclude() {
		$this->useFile( "pages/ui-include.xml" );
	}
	
	function testUiComposition() {
		$this->useFile( "pages/ui-composition.xml" );
		$this->useFile( "pages/ui-composition-jsfc.xml" );
	}
	
	function testUiRepeat() {
		$this->useFile( "pages/ui-repeat.xml" );
	}
	
	function testHDataTable() {
		//$this->useFile( "pages/h-dataTable.xml", true );
	}
	
	
	
	function useFile( $name, $writeResult = false ) {
		$originalTemplateData = file_get_contents( $name );
		
		$newTemplateData =& PhpFaces_FaceletsTemplateResource::ParseFacelets( $originalTemplateData, $this->smarty );
		if( $writeResult ) {
			file_put_contents( "$name.result", $newTemplateData );
		}
		$expectedResult = file_get_contents( "$name.result" );
		
		$this->assertEquals( $expectedResult, $newTemplateData, "Contents of output from $name were not equal to that of $name.result." );
	}
}

class FaceletsTest extends PHPUnit_TestCase {
	function __construct($name) {
		$this->PHPUnit_TestCase($name);
	}
	
	function setUp() {
		
		
		$this->facesContext =& new PhpFaces_Context_FacesContext();
		PhpFaces_Context_FacesContext::setCurrentInstance( $this->facesContext );
		$this->facesContext->registerBean( "testBean", "TestBean", PhpFaces_Context_FacesContext::SCOPE_REQUEST );
		
		$this->smarty = new Smarty();
		PhpFaces_setUp_smarty( $this->smarty );
		PhpFaces_FaceletsTemplateResource::register( $this->smarty );

	}
	
	function tearDown() {
		PhpFaces_tearDown_smarty( $this->smarty );
	}
	
	/*
	function testSmartyParsing() {
		$output = $this->smarty->fetch( "pages/smarty-friendliness.xml" );
		echo $output;
	}
	*/
	
	/*
	function testRawEL() {
		$this->useFile( "pages/raw-el.xml", true );
	}
	*/
	
	function testHInputText() {
		$this->useFile( "pages/h-inputText.xml" );
	}
	
	function testHOutputLabel() {
		$this->useFile( "pages/h-outputLabel.xml" );
	}
	
	function testHForm() {
		$this->useFile( "pages/h-form.xml" );
	}
	
	function testHDataTable() {
		$this->useFile( "pages/h-dataTable.xml", true );
	}
	
	function useFile( $name, $writeResult = false ) {
		$originalTemplateData = file_get_contents( $name );
		
		$newTemplateData =& $this->smarty->fetch( "facelets://$name" );
		if( $writeResult ) {
			file_put_contents( "$name.result", $newTemplateData );
		}
		$expectedResult = file_get_contents( "$name.result" );
		
		$this->assertEquals( $expectedResult, $newTemplateData, "Contents of output from $name were not equal to that of $name.result." );
	}
}

?>
